#include "RenderData.h"

namespace DreamEngine
{
CRenderData::CRenderData(void):
m_indexStream(MATH_NULL),
m_pMaterial(MATH_NULL)
{
} 


CRenderData::~CRenderData(void)
{
}

bool CRenderData::IsUseIndexBuffer()const
{
	return m_indexStream.size()>0;
}

bool CRenderData::IsEmpty()const
{
	return m_vertexStream.size()<=0;
}

CVertexBuffer* CRenderData::GetVertexBuffer(int index)const
{
	VertexStream::const_iterator iter = m_vertexStream.find(index);
	if (iter != m_vertexStream.end())
	{
		return (iter)->second;
	}

	return MATH_NULL;
}

void CRenderData::AddVertexBuffer(int index, CVertexBuffer* pVertexBuffer)
{
	m_vertexStream[index] = pVertexBuffer;
}

CIndexBuffer* CRenderData::GetIndexBuffer(int index)const
{
	if (index>=0 && index<=int(m_indexStream.size()))
	{
		return m_indexStream[index];
	}

	return MATH_NULL;
}

void CRenderData::AddIndexBuffer(CIndexBuffer* pIndexBuffer)
{
	m_indexStream.push_back(pIndexBuffer);
}

m_uint16 CRenderData::GetPatch()const
{
	return static_cast<m_uint16>(m_indexStream.size());
}

HardwareBufferType CRenderData::GetRenderType(int index)const
{
	if (index>=0 && index<int(m_indexStream.size()))
	{
		return m_indexStream[index]->GetType();
	}
	else if (m_vertexStream.size() > 0)
	{
		return (m_vertexStream.begin())->second->GetType();
	}

	return BUFFERTYPE_NULL;
}

m_uint16 CRenderData::GetElemCount(int index)const
{
	if (index>=0 && index<int(m_indexStream.size()))
	{
		return m_indexStream[index]->ElementCount();
	}
	else if (m_vertexStream.size() > 0)
	{
		return (m_vertexStream.begin())->second->ElementCount();
	}

	return 0;
}

m_uint16 CRenderData::CaculateTrangleCount()const
{
	m_uint16 trangleCount = 0;

	if (m_indexStream.size() > 0)
	{
		for (int idx=0; idx<int(m_indexStream.size()); ++idx)
		{
			trangleCount += m_indexStream[idx]->TrangleCount();
		}
	}
	else
	{
		VertexStream::const_iterator iter;
		for (iter=m_vertexStream.begin(); iter!=m_vertexStream.end(); ++iter)
		{
			trangleCount += iter->second->TrangleCount();
		}
	}

	return trangleCount;
}

void CRenderData::Destory()
{
	VertexStream::iterator iter;
	for (iter=m_vertexStream.begin(); iter!=m_vertexStream.end(); ++iter)
	{
		iter->second->Destory();
		SAFE_DELETE(iter->second);
	}
	m_vertexStream.clear();

	for (int i=0; i<int(m_indexStream.size()); ++i)
	{
		m_indexStream[i]->Destory();
		SAFE_DELETE(m_indexStream[i]);
	}
}

void CRenderData::AddElement(CVertexElement* pElement, m_uint16 count)
{
	if (pElement==MATH_NULL) return;

	for (int i=0; i<count; ++i)
	{
		m_elementLst.push_back(pElement[i]);
	}
}

}

